﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Caching;
using System.Web;



namespace Feelingme.ShareMSDN
{
    internal static class SharedUtil
    {
        public static IEnumerable GetProductList()
        {
            using (var dc = MSDNEntities.Create())
            {
                var products = dc.ProductTypes.ToArray();
                var families = dc.ProductNames;

                foreach (var e in products)
                {
                    e.Families = (from t in families
                                  where t.ProductTypeID == e.CategoryId
                                  orderby t.FamilyName
                                  select t)
#if DEBUG
                                  .Take(5)
#endif
                                  .ToArray();
                }

                return products;
            }
        }

        public static string GetHtmlFromResource(string htmlFile)
        {
            using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(
               "Feelingme.ShareMSDN." + htmlFile))
            {
                byte[] buffer = new byte[s.Length];
                s.Position = 0;
                s.Read(buffer, 0, (int)s.Length);
                return Encoding.UTF8.GetString(buffer);
            }
        }

        public static string GetShipsRawHTML(string template, int familyId, int locale, int sort)
        {
            const string ST = "<img style=\"border-width: 0px; align=\"absMiddle\" src=\"App_Themes/Classic/content/sort_{0}.gif\" alt=\"{0}\" />";
            Array source = null;
            int[] x = new int[] { 11, 20, 30 };
            string[] q = new string[3];


            using (var dc = MSDNEntities.Create())
            {
                var s = from t in dc.Items
                        where t.ProductNameID == familyId
#if RESTRICT
                        //&& t.DateUpdated < DateTime.Today.AddMonths(-1)
#endif
                        select t;

                if (locale != -1)
                    s = s.Where(p => p.LCID == locale);

                // sort:
                //  0   - no sort
                //  10  - DetailsName descending
                //  11  - DetailsName 
                //  20  - Name descending
                //  21  - Name 
                //  30  - Availabilty descending
                //  31  - Availabilty 
                if (sort != 0)
                    switch (sort / 10)
                    {
                        case 1:
                            if (sort % 2 == 0) { s = s.OrderByDescending(p => p.DetailsName); q[0] = string.Format(CultureInfo.InvariantCulture, ST, "desc"); }
                            else { s = s.OrderBy(p => p.DetailsName); x[0]++; q[0] = string.Format(CultureInfo.InvariantCulture, ST, "asc"); }
                            break;
                        case 2:
                            if (sort % 2 == 0) { s = s.OrderByDescending(p => p.LastUpdate); x[1]++; q[1] = string.Format(CultureInfo.InvariantCulture, ST, "desc"); }
                            else { s = s.OrderBy(p => p.LastUpdate); q[1] = string.Format(CultureInfo.InvariantCulture, ST, "asc"); }
                            break;
                        case 3:
                            if (sort % 2 == 0) { s = s.OrderByDescending(p => p.FileID); x[2]++; q[2] = string.Format(CultureInfo.InvariantCulture, ST, "desc"); }
                            else { s = s.OrderBy(p => p.FileID); q[2] = string.Format(CultureInfo.InvariantCulture, ST, "asc"); }
                            break;
                        default:
                            break;
                    }

                source = s.ToArray();
            }

            if (source.Length == 0)
                return string.Empty;


            return HtmlUtil.RenderRepeater(template, source, new
            {
                s00 = x[0],
                i00 = q[0],
                s01 = x[1],
                i01 = q[1],
                s02 = x[2],
                i02 = q[2],
            });
        }


    }
}
